package com.idea.zd.sys.train.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class FileOutputStreamTest {

    /**
     * FileOutputStream 字符输出流
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException{

        for (int i = 0; i <100 ; i++) {
            Write write = new Write(i);
            Thread thread = new Thread(write);
            thread.start();
        }
    }

    /**
     * 使用处理流
     * @throws IOException
     */
    public static void printStream() throws IOException{

        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("./src/main/java/com/idea/zd/sys/train/io/FileOutputStreamTest.txt");
        //创建处理流
        PrintStream printStream = new PrintStream(fileOutputStream);
        printStream.println("第一行");
        printStream.println("第二行");
        printStream.close();
        fileOutputStream.close();
    }
}

class Write implements Runnable{
    int i;
    Write(int i){
        this.i=i;
    }

    @Override
    public void run() {
        try{
            //创建输入流
            FileInputStream fileInputStream = new FileInputStream("./src/main/java/com/idea/zd/sys/train/io/FileOutputStreamTest.java");

            //创建输出流
            FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangzhenze/Documents/qxsjtb/Micaps/GridProc/Final/Rain/test/FileOutputStreamTest"+i+".txt");


            byte[] buff = new byte[1024];
            int hasRead = 0;

            //循环出数据
            while((hasRead = fileInputStream.read(buff)) > 0){

                //写入数据
                fileOutputStream.write(buff,0,hasRead);
            }

            fileInputStream.close();
            fileOutputStream.close();
            //使用处理流
            // printStream();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
